package com.jianneng.jdg.model.fragment;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.com.jianneng.jdg.R;
import com.jianneng.jdg.model.homeactivity.ActivitiesActivity;
import com.jianneng.jdg.model.homeactivity.FindFriendActivity;
import com.jianneng.jdg.model.homeactivity.LoginActivity;
import com.jianneng.jdg.model.homeactivity.MyNewActivity;
import com.jianneng.jdg.model.homeactivity.MyReleaseActivity;
import com.jianneng.jdg.model.homeactivity.PrefactActivity;
import com.jianneng.jdg.model.homeactivity.SetupActivity;

public class FragmentMyNews extends Fragment {
	private View view;
	private TextView mLogin, mPhotoGtaph, mUploadPhoto, mCancle;
	private String arguments = "";
	private LinearLayout mUserImage, mUserPerfect,mSetup;
	private ImageView mUserhead;
	private RelativeLayout mActivities,mMyNew,mMyRelease,mFindFriend;
	private static final int SELECT_IMAGE_REQUEST_CODE = 0;
	private static final int CAMERA_REQUESTCODE = 100;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_mynews, container, false);
		return view;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		if (getArguments() != null) {
			arguments = getArguments().getString("key");
			Toast.makeText(getActivity(), arguments + "", 1).show();
		}
		initView();

	}

	private void initView() {
		mLogin = (TextView) view.findViewById(R.id.login);
		mSetup = (LinearLayout) view.findViewById(R.id.ll_fragment_mynews_setup);
		mActivities = (RelativeLayout) view.findViewById(R.id.user_activities);
		mMyNew = (RelativeLayout) view.findViewById(R.id.myNew);
		mMyRelease = (RelativeLayout) view.findViewById(R.id.my_release);
		mFindFriend = (RelativeLayout) view.findViewById(R.id.find_friend);
		mLogin.setOnClickListener(new MyOnClickListener());
		// 模拟登陆成功
		if ("true".equals(arguments)) {
			mUserImage = (LinearLayout) view.findViewById(R.id.user_image);
			mUserPerfect = (LinearLayout) view.findViewById(R.id.user_perfect);
			mUserhead = (ImageView) mUserImage.findViewById(R.id.user_head);
			mUserhead.setOnClickListener(new MyOnClickListener());
			mUserPerfect.setOnClickListener(new MyOnClickListener());
			mUserImage.setVisibility(View.VISIBLE);
			mUserPerfect.setVisibility(View.VISIBLE);
			mLogin.setVisibility(View.GONE);
		}
		mSetup.setOnClickListener(new MyOnClickListener());
		mActivities.setOnClickListener(new MyOnClickListener());
		mMyNew.setOnClickListener(new MyOnClickListener());
		mMyRelease.setOnClickListener(new MyOnClickListener());
		mFindFriend.setOnClickListener(new MyOnClickListener());
		
	}
	public class MyOnClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.login:
				Intent intentLog = new Intent(getActivity(), LoginActivity.class);
				getActivity().startActivity(intentLog);
				break;
			case R.id.user_perfect :
				Intent intentPre = new Intent(getActivity(), PrefactActivity.class);
				getActivity().startActivity(intentPre);
				break;
			case R.id.user_head :
				//展示popwindow
				showPop();
				break;
			case R.id.ll_fragment_mynews_setup:
				Intent intentSet = new Intent(getActivity(), SetupActivity.class);
				getActivity().startActivity(intentSet);
				break;
			case R.id.user_activities://活动
				//Toast.makeText(getActivity(), "111", 1).show();
				Intent intentAct =new Intent(getActivity(), ActivitiesActivity.class);
				getActivity().startActivity(intentAct);
				break;
			case R.id.myNew :
				Intent intentNew = new Intent(getActivity(), MyNewActivity.class);
				getActivity().startActivity(intentNew);
				break;
			case R.id.my_release:
				Intent intentRel = new Intent(getActivity(), MyReleaseActivity.class);
				getActivity().startActivity(intentRel);
				break;
			case R.id.find_friend :
				Intent intentFind = new Intent(getActivity(), FindFriendActivity.class);
				getActivity().startActivity(intentFind);
				
				break;
			default:
				break;
			}
			
		}
		
	}

	public void showPop(){
        LayoutInflater inflater=LayoutInflater.from(getActivity());
        View v=inflater.inflate(R.layout.edit_photo,null);
        final PopupWindow window=new PopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
        window.setFocusable(true);
      
        //设置背景
        ColorDrawable dw = new ColorDrawable(R.color.white);
        window.setBackgroundDrawable(dw);

        //设置动画
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        //设置展现的位置
        window.showAtLocation(getActivity().findViewById(R.id.mynews), Gravity.BOTTOM,0,0);

        v.findViewById(R.id.photoGtaph).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            	// 1.只要知道意图
        		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        		// 存图片
        		if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                {
        		Uri uri = Uri.fromFile(new File(Environment
        				.getExternalStorageDirectory(), "34.jpg"));
        		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        		startActivityForResult(intent, CAMERA_REQUESTCODE);
                }else{
                	Toast.makeText(getActivity(),"没有内存卡",Toast.LENGTH_LONG).show();        
                }
        		window.dismiss();
            }
        });
        v.findViewById(R.id.upload_photo).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_PICK);
				intent.setType("vnd.android.cursor.dir/image");
				startActivityForResult(intent, SELECT_IMAGE_REQUEST_CODE);
				window.dismiss();
				
			}
		});
        v.findViewById(R.id.cancle).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				window.dismiss();
			}
		});
        window.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// 拍照回传
		if (requestCode == CAMERA_REQUESTCODE) {
			//从sd卡拿值q
			// 拿值
			File file = new File(Environment.getExternalStorageDirectory(), "34.jpg");
			Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
			//内存溢出，对图片进行压缩
			
			//Bitmap bitmap = data.getExtras().getParcelable("data");
			bitmap = zoomBitmap(bitmap,80,80);
			
			mUserhead.setImageBitmap(bitmap);

		}else if(requestCode == SELECT_IMAGE_REQUEST_CODE){
			Uri uri = data.getData();
			try {
				Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
				bitmap = zoomBitmap(bitmap,80,80);
				mUserhead.setImageBitmap(bitmap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	//对图片压缩
	private  Bitmap zoomBitmap(Bitmap bitmap,int width,int height){
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		//比例
		float sx =((float) width)/w;
		float sy =((float) height)/h;
		//矩阵
		Matrix matrix = new Matrix();
		matrix.postScale(sx, sy);
		//重新创建
		bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
		return bitmap;
		
	}
}
